<?php
/*
 * @Description    : 通用
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2021-04-17 09:01:53
 * @LastEditors    : QianLong
 * @LastEditTime   : 2022-07-01 17:45:24
 */

namespace app\api\controller;

use app\api\model\Manager;
use app\common\service\AliOss;
use app\common\Verify;
use app\lib\Result;
use think\facade\Request;

class Common extends Base
{
  public function upload()
  {
    // 获取表单上传文件
    $files = request()->file();
    try {
      validate(['image' => 'filesize:10240|fileExt:jpg|image:200,200,jpg'])
        ->check($files);
      $savename = [];
      foreach ($files as $file) {
        $savename[] = \think\facade\Filesystem::putFile('topic', $file);
      }
    } catch (\think\exception\ValidateException $e) {
      echo $e->getMessage();
    }
  }
  public function uploadFile()
  {
    header('Content-Type:application/json; charset=utf-8');
    if (request()->isPost()) {
      $file = request()->file();
      $type = input('param.type');
      $info = validate(['image' => 'filesize:30720|fileExt:jpg,png,gif,jpeg,bmp,doc,docx,xls,xlsx,ppt,pptx,zip,rar,txt,mp4,avi,flv'])->check($file);
      if ($info) {
        //基于框架根目录的文件路径
        $savename = [];
        foreach ($file as $ins) {
          $savename[] = \think\facade\Filesystem::disk('public')->putFile('topic', $ins);
        }
        $fileUrls = [];
        $domain = Request::domain();
        foreach ($savename as $file) {
          $file = str_replace('\\','/', $file);
          $fileUrls[] = $domain . '/storage/' . $file;
        }
        $ossClass = new AliOss();
        $ossConfig = $ossClass->getConfig();
        if ($ossConfig['open'] !== 1) {
          if ($type == 'wangeditor') {
            $return['errno'] = 0;
            $return['data'] = [];
            foreach ($fileUrls as $file) {
              $return['data'][] = ['url' => $file];
            }
          } else {
            $return['code'] = 200;
            $return['data'] = $fileUrls;
          }
        } else {
          $ossFileUrls = [];
          foreach ($savename as $file) {
            $file = str_replace('\\','/', $file);
            $ossClass->uploadFile('qianlong.admin/' . $file, UPLOAD_PATH . $file);
            unlink(UPLOAD_PATH . $file);
            $ossFileUrls[] = $ossClass->getOssUrl('qianlong.admin/' . $file);
          }
          if ($type == 'wangeditor') {
            $return['errno'] = 0;
            $return['data'] = [];
            foreach ($ossFileUrls as $file) {
              $return['data'][] = ['url' => $file];
            }
          } else {
            $return['code'] = 200;
            $return['data'] = $ossFileUrls;
          }
        }
        return json($return);
      } else {
        var_dump($info);
        var_dump($file);
      }
    }
  }
  public function uploadCrt()
  {
    header('Content-Type:application/json; charset=utf-8');
    if (request()->isPost()) {
      $file = request()->file();
      $info = validate(['application' => 'fileExt:crt'])->check($file);
      if ($info) {
        //基于框架根目录的文件路径
        $savename = [];
        foreach ($file as $ins) {
          $savename[] = \think\facade\Filesystem::disk('crt')->putFile('file', $ins);
        }
        return json(['code' => 200, 'data' => $savename[0], 'msg' => 'success']);
      } else {
        var_dump($info);
        var_dump($file);
        // 上传失败获取错误信息
      }
    }
  }
}
